/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap/bootstrap')

/**
 * Vue is a modern JavaScript library for building interactive web interfaces
 * using reactive data binding and reusable components. Vue's API is clean
 * and simple, leaving you to focus on building your next great project.
 */

window.Vue = require('vue')
import Util from './libs/util'
/**
 * 自定义组件
 */
window.ModelDataSource = require('./libs/ModelDataSource')
Vue.use(ModelDataSource)
window.ListDataSource = require('./libs/ListDataSource')
Vue.use(ListDataSource)
/**
 * Vuex
 */
import store from './store'

/**
 * Vue Router
 */
import router from './bootstrap/router'

// router.beforeEach((to, from, next) => {
//   // ...
//   console.log(to)
//   if (to.name === 'select-role') {
//     next()
//   } else if (store.state.user.type === '0') {
//     next({name: 'select-role'})
//   } else if (to.name !== 'personal-info' && store.state.user.is_complete === 0) {
//     next({name: 'personal-info'})
//   } else {
//     next()
//   }
// })

/**
 * Element-UI
 */

import ElementUI from 'element-ui'

Vue.use(ElementUI)
/**
 * 全局 请求错误 响应
 */
window.axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response
}, function (error) {
  switch (error.response.status) {
    case 401:
      Util.logout()
      break
    case 422:
      app.$message.error(error.response.data.message)
      break
    case 481:
      app.$message.error(error.response.data.message)
      break
  }
  return Promise.reject(error)
})

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 *
 * 全局组件
 */

// register globally

Vue.component('modal', require('./components/Modal.vue'))
Vue.component('confirm-modal', require('./components/ConfirmModal.vue'))
Vue.component('pagination', require('./components/Pagination.vue'))
Vue.component('cplanet-pagination', require('./components/CplanetPagination.vue'))
Vue.component('vue-table', require('./components/Table.vue'))
Vue.component('el-page', require('./components/Page.vue'))



Vue.component('app', require('./app.vue'))

let app = new Vue({
  el: '#app',
  router: router,
  store: store,
  template: '<app/>'
})
